<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class WebController extends Controller {
    // 首页
    public function index(){
        layout(false);
        $this->init();
        // 获取主要标题
        $indexweb=M('indexweb');
        $info=$indexweb->where("id=1")->select();
        $this->assign('main_title',$info[0]['title']);
        $index_silde=json_decode($info[0]['index_silde']);
        $this->assign('index_silde',$index_silde);
        // 获取最新的六条作品
        $article=M('article');
        $articleList=$article->order('time')->limit(6)->select();
        foreach ($articleList as $key => $value) {
            $articleList[$key]['main_img']=__ROOT__."/".$value['main_img'];
        }
        
        $this->assign('articleList',$articleList);
		$this->display();
    }

    // 联系我们
    public function contact(){
        layout(false);
        $this->init();
        $this->display();
    }
     // 上传评论
    public function commentSave(){
        $comment=M('comment');
        $data['username'] = I('post.username');
        $data['content'] = I('post.content');
        $data['mail'] =I('post.mail');
        $data['time'] = time();
        // var_dump($data);
        $saveState=$comment->add($data);
        if($saveState){
            $this->success("提交成功",U("Web/contact"));
        }else{
            $this->error('提交失败');
        }
    }
   public function commentUpload(){
        $comment=M('comment');
        $data['username'] = I('post.username');
        $data['content'] = I('post.content');
        $data['mail'] =I('post.mail');
        $data['time'] = time();
        // var_dump($data);
        $storageState=$comment->add($data);
        if($storageState){
            $this->success("提交成功",U("Web/article_share"));
        }else{
            $this->error('提交失败');
        }
    }
    // 文章分享
    public function article_share(){
        layout(false);
        $this->init();
        if(isset($_POST['username'])){
            $this->commentUpload();
        }
         // 获取最新的六条评论
        $comment=M('comment');
        $commentList=$comment->order('time desc')->limit(6)->select();
        foreach ($commentList as $key => $value) {
            $commentList[$key]['time']=date("Y-m-d H:i:s",$value['time']);
        }
        $this->assign('commentList',$commentList);
        // 获取最新的六条作品
        $article=M('article');
        $type=isset($_GET['type'])?$_GET['type']:"web";
        if($type=="interface1"){
        	$type="interface";
        }
        $data['type']=$type;
        $articleList=$article->where($data)->order('time desc')->limit(6)->select();
        foreach ($articleList as $key => $value) {
            $articleList[$key]['main_img']=__ROOT__."/".$value['main_img'];
            // 转换为HTML代码
            $articleList[$key]['content']=html_entity_decode($value['content']);
         }
//        var_dump($articleList);
        $recommendedList=$article->where($data)->order('recommended desc')->limit(3)->select();
		$this->assign('articleList',$articleList);
		$this->assign('recommendedList',$recommendedList);
        $this->display();
    }
    public function ajax_article(){
        // var_dump($_POST);
        $article=M('article');
        $data['Id']=I("post.id");
        $article_main=$article->where($data)->select();
        // echo json_encode($work[0]);
        $article_main[0]['content']=html_entity_decode($article_main[0]['content']);
        $this->ajaxReturn($article_main[0]);
    }

    // 素材收集
    public function material_collection(){
        layout(false);
        $this->init();
        $this->display();
    }


    // 作品分享
    public function works_share(){
        layout(false);
        $this->init();
        // 获取最新的六条作品
        $works=M('works');
        $type=isset($_GET['type'])?$_GET['type']:"web";
        $data['type']=$type;
        $workList=$works->where($data)->order('time desc')->limit(16)->select();
        foreach ($workList as $key => $value) {
            $workList[$key]['main_img']=__ROOT__."/".$value['main_img'];
            // 转换为HTML代码
            $workList[$key]['content']=html_entity_decode($value['content']);
        }
        $this->assign('workList',$workList);
        $this->display();
    }
    public function ajax_work(){
        // var_dump($_POST);
        $works=M('works');
        $data['Id']=I("post.id");
        $work=$works->where($data)->select();
        // echo json_encode($work[0]);
        $work[0]['content']=html_entity_decode($work[0]['content']);
        $this->ajaxReturn($work[0]);
    }


    public function init(){
        $indexweb=M('indexweb');
        $info=$indexweb->where("id=1")->select();
        $this->assign('logo_img',__ROOT__."/".$info[0]['logo_img']);
    }
}